<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\command;

use app\model\mongo\MongoKsUserVideoModel;
use app\model\mongo\MongoKsUserVideoStatModel;
use think\console\Input;
use think\console\Output;

class TestRepairAppendCostWork extends BaseWork
{
    protected function configure()
    {
        // 指令配置
        $this->setName('test_repair_append_cost_work');
        // 设置参数
        $this->setDescription('test-扫描消耗修复任务[废弃]')
            ->setHelp('php think test_repair_append_cost_work');
    }

    protected function execute(Input $input, Output $output)
    {
        set_time_limit(0);
        ini_set('memory_limit', '-1');
        $output->writeln('=====================扫描消耗修复任务开始' . date('Y-m-d H:i:s') . '=====================');
        $list = (new MongoKsUserVideoModel())->where('is_sync', 0)->limit(10000)->select();

        if (count($list) <= 0) {
            $output->writeln('没有需要同步的数据');
            return;
        }

        foreach ($list as $v) {
            $info = $this->_getKsVideoStatInfo($v['user_id'], $v['photo_id']);

            if (! $info) {
                $res = (new MongoKsUserVideoStatModel())->insert([
                    'video_id' => $v['photo_id'],
                    'signature' => $v['signature'],
                    'user_id' => $v['user_id'],
                    'cost' => 0,
                    'create_time' => time(),
                    'update_time' => time(),
                ]);

                if ($res <= 0) {
                    $output->writeln('插入失败');
                }
                $output->writeLn('插入成功');
            }
            (new MongoKsUserVideoModel())->where('_id', $v['_id'])->update(['is_sync' => 1]);
        }

        $output->writeln('=====================扫描消耗修复任务结束' . date('Y-m-d H:i:s') . '=====================');
    }

    private function _getKsVideoStatInfo(string $userId = '', string $videoId = '')
    {
        return (new MongoKsUserVideoStatModel())->where('user_id', $userId)->where('video_id', $videoId)->find();
    }
}
